package cn.uniquetech.esf;

import cn.uniquetech.util.FileUtil;
import cn.uniquetech.util.SqlJavaType;

import java.util.List;

public class GenNewViewUtil {
	// 生成view试图 sb.append(""+tailStr);
	private static String htmlTailStr = "\r\n";
	private static String key = "iid";
	// 要排除的字段
	private static String fields = ",iid,icreator,dcreator,ccreator,imodify,dmodify,cmodify,";

	public static String paraseView(String tableName, String voName, String tableRemarks, List<ColumnVO> columnList) {

		String packageName = GenerateCodeSqlServer.PACKAGENAME;
		StringBuffer sb = new StringBuffer();
		sb.append("<!DOCTYPE html>" + htmlTailStr);

		sb.append("<html lang=\"zh-cn\" xmlns:th=\"http://www.thymeleaf.org\">" + htmlTailStr);
		sb.append("<head th:include=\"header::header\"/>" + htmlTailStr);

		/*sb.append("<%" + htmlTailStr);
		sb.append("String path = request.getContextPath();" + htmlTailStr);
		sb.append("String basePath = request.getScheme() + \"://\" + request.getServerName() + \":\" + request.getServerPort() + path;" + htmlTailStr);
		sb.append("%>" + htmlTailStr);*/

		/*sb.append("<!DOCTYPE html>" + htmlTailStr + "<html>" + htmlTailStr + "<head>" + htmlTailStr);
		sb.append("<%@include file=\"/view/head.jsp\"%>" + htmlTailStr);
		sb.append("<script type=\"text/javascript\" src=\"<%=basePath%>/static/js/GridViewUtil.js\"></script>" + htmlTailStr);
		sb.append("</head>" + htmlTailStr + "<body>" + htmlTailStr);*/
		sb.append("<body>");
//		sb.append("<div id=\"dgtoolbar\" class=\"datagrid-toolbar\"><%@include file=\"/view/GridToolBar.jsp\"%></div>" + htmlTailStr);
		sb.append("<div id=\"dgtoolbar\" class=\"datagrid-toolbar\">" + htmlTailStr);
		sb.append("<a id=\"newbtn\" href=\"#\" class=\"easyui-linkbutton\" iconCls=\"icon-add\" plain=\"true\" title=\"新增\">新增</a>" + htmlTailStr);
		sb.append("<a id=\"deletebtn\" href=\"#\" class=\"easyui-linkbutton\" iconCls=\"icon-remove\" plain=\"true\" title=\"批量删除\">删除</a>" + htmlTailStr);
		sb.append("<a id=\"reloadbtn\" href=\"#\" class=\"easyui-linkbutton\" iconCls=\"icon-reload\" plain=\"true\">刷新</a>" + htmlTailStr);
		sb.append("<a id=\"querybtn\" href=\"#\" class=\"easyui-linkbutton\" iconCls=\"icon-search\" plain=\"true\" title=\"查询\">查询</a>" + htmlTailStr);
		sb.append("</div>" + htmlTailStr);

		// --------------
		sb.append("<table id=\"dg\" class=\"easyui-datagrid\" data-options=\"url:'pageQuery.do',idField:'" + key + "',rownumbers:true,singleSelect:false,method:'post',toolbar:'#dgtoolbar',pagination:true,pageSize:20,pageList:[20,30,50],fit:true,onLoadSuccess:onDGLoadSuccess\">" + htmlTailStr);
		sb.append("<thead><tr>" + htmlTailStr);
		sb.append("<th data-options=\"field:'" + key + "'\" checkbox=\"true\" />" + htmlTailStr);
		int colSize = columnList.size();
		for (int i = 0; i < colSize; i++) {
			String colName = columnList.get(i).getColName().trim();
			String remark = columnList.get(i).getColRemarks().trim();
			String colType = SqlJavaType.sqlType2JavaType(columnList.get(i).getColType().trim());
			// System.out.println("coltype------>" + colType);
			if (key.equals(colName)) {
				continue;
			}
			if (fields.indexOf("," + colName + ",") != -1) {
				continue;
			}
			if ("Date".equalsIgnoreCase(colType)) {
				sb.append("<th data-options=\"field:'" + colName + "'\" width=\"60\" align=\"center\" formatter=\"Common.dateTime\" >" + remark + "</th>" + htmlTailStr);
			} else {
				sb.append("<th data-options=\"field:'" + colName + "'\" width=\"60\" align=\"center\">" + remark + "</th>" + htmlTailStr);
			}
		}

		sb.append("<th data-options=\"field:'opt',formatter:onItemOptEdit\" width=\"40\" align=\"center\">修改</th>" + htmlTailStr);
		sb.append("<th data-options=\"field:'opt1',formatter:onItemOptDelete\" width=\"40\" align=\"center\">删除</th>" + htmlTailStr);
		sb.append("<th data-options=\"field:'opt2',formatter:onItemOptShow\" width=\"40\" align=\"center\">查看</th>" + htmlTailStr);
		sb.append("</tr></thead></table>" + htmlTailStr);

		// -------------新增，修改
		sb.append("<div id=\"dlg\" class=\"easyui-dialog\" title=\"操作\" style=\"padding:5px\" data-options=\"buttons: '#dlg-buttons',closed:'true',modal:true\">" + htmlTailStr);
		sb.append("<form id=\"dataform\" method=\"post\">" + htmlTailStr + "<table>" + htmlTailStr);
		sb.append("<tr><td><input name=\"" + key + "\" id=\"" + key + "\" type=\"hidden\"></input></td>" + htmlTailStr + "</tr>" + htmlTailStr);

		for (int i = 0; i < colSize; i++) {

			ColumnVO columnVO = columnList.get(i);
			String colName = columnVO.getColName().trim();
			String colRemark = columnVO.getColRemarks().trim();
			String colType = SqlJavaType.sqlType2JavaType(columnVO.getColType());
			String isNull = columnVO.getIsNull();

			if (key.equals(colName)) {
				continue;
			}

			if (fields.indexOf("," + colName + ",") != -1) {
				continue;
			}

			if ("isort".equals(colName)) {
				sb.append("<tr><td>" + colRemark + ":</td><td><input name=\"" + colName + "\" class=\"easyui-numberspinner\" style=\"width: 200px\" data-options=\"min:0,max:1000,editable:false\" " + ("0".equals(isNull) ? "required=\"required\"" : "") + "/></td></tr>" + htmlTailStr);
			} else {

				if ("String".equals(colType)) {
					sb.append("<tr><td>" + colRemark + ":</td><td><input name=\"" + colName + "\" class=\"easyui-textbox\" style=\"width: 200px\" " + ("0".equals(isNull) ? "required=\"required\"" : "") + "/></td></tr>" + htmlTailStr);
				} else if ("Date".equals(colType)) {
					sb.append("<tr><td>" + colRemark + ":</td><td><input name=\"" + colName + "\" class=\"easyui-datebox\" style=\"width: 200px\" " + ("0".equals(isNull) ? "required=\"required\"" : "") + "/></td></tr>" + htmlTailStr);
				} else {
					sb.append("<tr><td>" + colRemark + ":</td><td><input name=\"" + colName + "\" class=\"easyui-textbox\" style=\"width: 200px\" " + ("0".equals(isNull) ? "required=\"required\"" : "") + "/></td></tr>" + htmlTailStr);
				}
			}
		}
		sb.append("</table></form></div>");

		sb.append("<div id=\"dlg-buttons\">" + htmlTailStr);
		sb.append("<a id=\"dlg-savebtn\" 	href=\"javascript:void(0)\" style=\"width:70px\" class=\"easyui-linkbutton\" iconCls=\"icon-ok\">保存</a>" + htmlTailStr);
		sb.append("<a id=\"dlg-closebtn\" 	href=\"javascript:void(0)\" style=\"width:70px\" class=\"easyui-linkbutton\" iconCls=\"icon-no\">关闭</a>" + htmlTailStr);
		sb.append("</div>" + htmlTailStr);

		// -------------查询
		sb.append("<div id=\"dlg-query\" class=\"easyui-dialog\" title=\"查询\" style=\"padding:5px\" data-options=\"buttons: '#dlg-query-buttons',closed:'true',modal:true\">" + htmlTailStr);
		sb.append("<form id=\"dataform-query\" method=\"post\">" + htmlTailStr + "<table>" + htmlTailStr);
		// sb.append("<tr><td><input name=\"" + key + "\" id=\"" + key +
		// "\" type=\"hidden\"></input>" + htmlTailStr + "</tr>" + htmlTailStr);

		for (int i = 0; i < colSize; i++) {

			ColumnVO columnVO = columnList.get(i);
			String colName = columnVO.getColName().trim();
			String colRemark = columnVO.getColRemarks().trim();
			String colType = SqlJavaType.sqlType2JavaType(columnVO.getColType());
			String isNull = columnVO.getIsNull();

			if (key.equals(colName)) {
				continue;
			}

			if (fields.indexOf("," + colName + ",") != -1) {
				continue;
			}

			if ("isort".equals(colName)) {
				sb.append("<tr><td>" + colRemark + ":</td><td><input name=\"" + colName + "\" class=\"easyui-numberspinner\" style=\"width: 200px\" data-options=\"min:0,max:1000,editable:false\" " + ("0".equals(isNull) ? "required=\"required\"" : "") + "/></td></tr>" + htmlTailStr);
			} else {

				if ("String".equals(colType)) {
					sb.append("<tr><td>" + colRemark + ":</td><td><input name=\"" + colName + "\" class=\"easyui-textbox\" style=\"width: 200px\" " + ("0".equals(isNull) ? "required=\"required\"" : "") + "/></td></tr>" + htmlTailStr);
				} else if ("Date".equals(colType)) {
					sb.append("<tr><td>" + colRemark + ":</td><td><input name=\"" + colName + "\" class=\"easyui-datebox\" style=\"width: 200px\" " + ("0".equals(isNull) ? "required=\"required\"" : "") + "/></td></tr>" + htmlTailStr);
				} else {
					sb.append("<tr><td>" + colRemark + ":</td><td><input name=\"" + colName + "\" class=\"easyui-textbox\" style=\"width: 200px\" " + ("0".equals(isNull) ? "required=\"required\"" : "") + "/></td></tr>" + htmlTailStr);
				}
			}
		}
		sb.append("</table></form></div>");
		sb.append("<div id=\"dlg-query-buttons\">" + htmlTailStr);
		sb.append("<a id=\"dlg-query-savebtn\" 	href=\"javascript:void(0)\" style=\"width:70px\" class=\"easyui-linkbutton\" iconCls=\"icon-search\">查询</a>" + htmlTailStr);
		sb.append("<a id=\"dlg-query-closebtn\" 	href=\"javascript:void(0)\" style=\"width:70px\" class=\"easyui-linkbutton\" iconCls=\"icon-no\">关闭</a>" + htmlTailStr);
		sb.append("</div>" + htmlTailStr);

		sb.append("<script type=\"text/javascript\" th:src=\"@{/static/common/GridView.js}\"></script>");
		sb.append("</body></html>");
		String clazz = voName.toLowerCase();
		String servicePackage = packageName + ".html." + clazz;
		FileUtil.writeFile(servicePackage, "index.html", sb.toString());
		return sb.toString();
	}
}
